<?php

/**
 * This is the model class for table "tbl_feedback".
 *
 * The followings are the available columns in table 'tbl_feedback':
 * @property integer $id
 * @property integer $user_id_post
 * @property integer $user_id_bid
 * @property integer $job_id
 * @property string $comment
 * @property integer $time_stone
 * @property integer $quality_rate
 * @property double $rating
 *
 * The followings are the available model relations:
 * @property Users $userIdPost
 * @property Users $userIdB
 * @property Job $job
 */
class Feedback extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Feedback the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_feedback';
	}

	protected function beforeValidate(){
		if ($this->isNewRecord) {
			$this->time_stone = 0;
			$this->quality_rate = 0;
		}
		return parent::beforeValidate();
	}
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('comment,rating', 'required'),
			array('user_id_post, user_id_bid, job_id, time_stone, quality_rate', 'numerical', 'integerOnly'=>true),
			array('rating', 'numerical'),
			array('comment', 'length', 'max'=>256),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id_post, user_id_bid, job_id, comment, time_stone, quality_rate, rating', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'userIdPost' => array(self::BELONGS_TO, 'Users', 'user_id_post'),
			'userIdB' => array(self::BELONGS_TO, 'Users', 'user_id_bid'),
			'job' => array(self::BELONGS_TO, 'Job', 'job_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id_post' => 'User Id Post',
			'user_id_bid' => 'User Id Bid',
			'job_id' => 'Job',
			'comment' => 'Comment',
			'time_stone' => 'Time Stone',
			'quality_rate' => 'Quality Rate',
			'rating' => 'Rating',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id_post',$this->user_id_post);
		$criteria->compare('user_id_bid',$this->user_id_bid);
		$criteria->compare('job_id',$this->job_id);
		$criteria->compare('comment',$this->comment,true);
		$criteria->compare('time_stone',$this->time_stone);
		$criteria->compare('quality_rate',$this->quality_rate);
		$criteria->compare('rating',$this->rating);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}